/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "MapVC.h"
#import <WebKit/WebKit.h>
#import "CabDriver.h"

@implementation MapVC
@synthesize cabList;
@synthesize convertLong;
@synthesize convertLat;
- (id)init
{
    [super init];
    mapView = [[WebView alloc] init];
    return self;
}


-(void)awakeFromNib //Once the window gets pulled up, a google map gets pulled up from the server.
{
	_timer = [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(reload) userInfo:nil repeats:YES];
	[_timer fire];
	
	NSURL *mapFileURL = [NSURL URLWithString:@"http://sunna-server.appspot.com/html/cabMap.html"];
	//NSURL *mapFileURL = [NSURL URLWithString:@"http://www.google.com"];
	[[mapView mainFrame] loadRequest:[NSURLRequest requestWithURL:mapFileURL]];	 
	[[[mapView mainFrame] frameView] setAllowsScrolling:NO];	
	[mapView setNeedsDisplay:YES];
	NSLog(@"This is map : %@", [CabDriver allCabDrivers]);
	self.cabList = [CabDriver allCabDrivers];
	id scriptObject = [mapView windowScriptObject];
	
    NSString* result = [scriptObject evaluateWebScript:@"14*Math.sin(0.3)"];
	[scriptObject evaluateWebScript:@"placeMarkers"];
/*
						
	
	NSLog(@" %@", result);
	NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
	[f setNumberStyle:NSNumberFormatterDecimalStyle];
		
	for(int i =0 ; i < [cabList count]; i++){
		CabDriver* cabDriver = [cabList objectAtIndex:i];
		NSLog(@"%d",i);
		NSLog(@"%@", [cabDriver latitude]);
		NSLog(@"%@", [cabDriver longitude]);
		
		convertLat = [f numberFromString:[cabDriver latitude]];
		convertLong =[f numberFromString:[cabDriver longitude]];
	
		NSLog(@"flot lat %f", [cabDriver latitude]);
		NSLog(@"flot long %f", [cabDriver longitude]);
		
		NSArray *args = [NSArray arrayWithObjects:
						convertLat, convertLat,
						 nil];
		
		[scriptObject callWebScriptMethod:@"placeMarkers"
				   withArguments:args];
		
	}
	[f release];
	*/

}


- (void)reload
{
	  [[mapView mainFrame] reload];
	
}


@end
